﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace CardLogistics.Web.MainPage.Export
{
    public partial class DataExport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string s = Request.QueryString["s"];
            if (s != null && s.Trim().Length > 0)
            {
                DataTable table = DAL.DbHelperSQL.GetQueryResult(s);
                if (table.Rows.Count > 0)
                {
                    gvList.DataSource = table;
                    gvList.DataBind();
                    Random rdm = new Random(unchecked((int)DateTime.Now.Ticks));
                    //ExportExcel(rdm.Next(1, 10000).ToString());
                    ExportExcel(this.gvList, rdm.Next(1, 10000).ToString());
                }
                else
                    Response.Write("没有数据可导出");
            }
        }
        /// <summary>
        /// 导出报表
        /// </summary>
        /// <param name="strFileName">文件名</param>
        public void ExportExcel(Control ctl, string strFileName)
        {
            HttpContext.Current.Response.Charset = "UTF-8";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "online; filename=" + strFileName + ".xls");
            ctl.Page.EnableViewState = false;
            System.IO.StringWriter tw = new System.IO.StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            ctl.RenderControl(hw);
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.End();
        }
        /// <summary>
        /// 导出报表
        /// </summary>
        /// <param name="strFileName">文件名</param>
        public void ExportExcel_01(string strFileName)
        {
            strFileName = System.Web.HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8);
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.Buffer = true;
            System.Web.HttpContext.Current.Response.Charset = "UTF-8";
            System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "online; filename=" + strFileName + ".xls");
            System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
            System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";

            System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("zh-CN", true);
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

            gvList.RenderControl(oHtmlTextWriter);

            System.Web.HttpContext.Current.Response.Write(oStringWriter.ToString().Replace("<td", "<td STYLE='FONT-SIZE:14px;MSO-NUMBER-FORMAT:\\@'")
                                                          .Replace("#B4CDEB", "#000000"));
            System.Web.HttpContext.Current.Response.Buffer = false;
            System.Web.HttpContext.Current.Response.End();
        }
        public override void VerifyRenderingInServerForm(Control control)
        {
        }
    }
}